

<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  
  <title>S3 的 C++ 接口实例 &mdash; Ceph Documentation</title>
  

  
  <link rel="stylesheet" href="../../../_static/ceph.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/graphviz.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/css/custom.css" type="text/css" />

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  

  
  

  

  
  <!--[if lt IE 9]>
    <script src="../../../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script src="../../../_static/jquery.js"></script>
        <script src="../../../_static/underscore.js"></script>
        <script src="../../../_static/doctools.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    
    <link rel="index" title="Index" href="../../../genindex/" />
    <link rel="search" title="Search" href="../../../search/" />
    <link rel="next" title="C# S3 Examples" href="../csharp/" />
    <link rel="prev" title="对象操作" href="../objectops/" /> 
</head>

<body class="wy-body-for-nav">

   
  <header class="top-bar">
    

















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../" class="icon icon-home"></a> &raquo;</li>
        
          <li><a href="../../">Ceph 对象网关</a> &raquo;</li>
        
          <li><a href="../">Ceph 对象网关的 S3 API</a> &raquo;</li>
        
      <li>S3 的 C++ 接口实例</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../../../_sources/radosgw/s3/cpp.rst.txt" rel="nofollow"> View page source</a>
          
        
      </li>
    
  </ul>

  
  <hr/>
</div>
  </header>
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search"  style="background: #eee" >
          

          
            <a href="../../../">
          

          
            
            <img src="../../../_static/logo.png" class="logo" alt="Logo"/>
          
          </a>

          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../../../start/intro/">Ceph 简介</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../install/">安装 Ceph</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../cephadm/">Cephadm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../rados/">Ceph 存储集群</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../cephfs/">Ceph 文件系统</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../rbd/">Ceph 块设备</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../../">Ceph 对象网关</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="../../frontends/">HTTP 前端</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../placement/">存储池归置与存储类</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../multisite/">多站配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../multisite-sync-policy/">多站同步策略配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../pools/">存储池的配置</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../config-ref/">配置参考</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../admin/">管理指南</a></li>
<li class="toctree-l2 current"><a class="reference internal" href="../">S3 API</a><ul class="current">
<li class="toctree-l3 current"><a class="reference internal" href="../#api">API</a><ul class="current">
<li class="toctree-l4"><a class="reference internal" href="../commons/">常见的</a></li>
<li class="toctree-l4"><a class="reference internal" href="../authentication/">认证</a></li>
<li class="toctree-l4"><a class="reference internal" href="../serviceops/">Service Ops</a></li>
<li class="toctree-l4"><a class="reference internal" href="../bucketops/">Bucket Ops</a></li>
<li class="toctree-l4"><a class="reference internal" href="../objectops/">Object Ops</a></li>
<li class="toctree-l4 current"><a class="current reference internal" href="#">C++</a></li>
<li class="toctree-l4"><a class="reference internal" href="../csharp/">C#</a></li>
<li class="toctree-l4"><a class="reference internal" href="../java/">Java</a></li>
<li class="toctree-l4"><a class="reference internal" href="../perl/">Perl</a></li>
<li class="toctree-l4"><a class="reference internal" href="../php/">PHP</a></li>
<li class="toctree-l4"><a class="reference internal" href="../python/">Python</a></li>
<li class="toctree-l4"><a class="reference internal" href="../ruby/">Ruby AWS::SDK Examples (aws-sdk gem ~&gt;2)</a></li>
<li class="toctree-l4"><a class="reference internal" href="../ruby/#ruby-aws-s3-examples-aws-s3-gem">Ruby AWS::S3 Examples (aws-s3 gem)</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="../#id1">功能支持情况</a></li>
<li class="toctree-l3"><a class="reference internal" href="../#header">不支持的 Header 字段</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="../../rgw-cache/">Data caching and CDN</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../swift/">Swift API</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../adminops/">管理操作 API</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../api/">Python 接口</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../nfs/">通过 NFS 导出</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../keystone/">与 OpenStack Keystone 对接</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../barbican/">与 OpenStack Barbican 对接</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../vault/">与 HashiCorp Vault 对接</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../kmip/">KMIP Integration</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../opa/">与 Open Policy Agent 对接</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../multitenancy/">多租户</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../compression/">压缩</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../ldap-auth/">LDAP 认证</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../encryption/">服务器端加密</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../bucketpolicy/">桶策略</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../dynamicresharding/">动态的桶索引重分片</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../mfa/">多因子认证</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../sync-modules/">同步模块</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../notifications/">Bucket Notifications</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../layout/">RADOS 中的数据布局</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../STS/">STS</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../STSLite/">STS Lite</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../keycloak/">Keycloak</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../role/">Role</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../session-tags/">Session Tags</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../orphans/">Orphan List and Associated Tooliing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../oidc/">OpenID Connect Provider</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../troubleshooting/">故障排除</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../man/8/radosgw/">radosgw 手册页</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../../man/8/radosgw-admin/">radosgw-admin 手册页</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../qat-accel/">使用 QAT 为加密和压缩提速</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../s3select/">S3-select</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../lua-scripting/">Lua Scripting</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../d3n_datacache/">D3N Data Cache</a></li>
<li class="toctree-l2"><a class="reference internal" href="../../cloud-transition/">Cloud Transition</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../../../mgr/">Ceph 管理器守护进程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../mgr/dashboard/">Ceph 仪表盘</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../api/">API 文档</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../architecture/">体系结构</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../dev/developer_guide/">开发者指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../dev/internals/">Ceph 内幕</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../governance/">项目管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../foundation/">Ceph 基金会</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../ceph-volume/">ceph-volume</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../releases/general/">Ceph 版本（总目录）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../releases/">Ceph 版本（索引）</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../security/">Security</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../glossary/">Ceph 术语</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../jaegertracing/">Tracing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../translation_cn/">中文版翻译资源</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../">Ceph</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
<div id="dev-warning" class="admonition note">
  <p class="first admonition-title">Notice</p>
  <p class="last">This document is for a development version of Ceph.</p>
</div>
  <div id="docubetter" align="right" style="padding: 5px; font-weight: bold;">
    <a href="https://pad.ceph.com/p/Report_Documentation_Bugs">Report a Documentation Bug</a>
  </div>

  
  <div class="section" id="s3-c">
<span id="cpp"></span><h1>S3 的 C++ 接口实例<a class="headerlink" href="#s3-c" title="Permalink to this headline">¶</a></h1>
<div class="section" id="id1">
<h2>配置<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p>The following contains includes and globals that will be used in later examples:</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&quot;libs3.h&quot;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;stdlib.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;iostream&gt;</span><span class="cp"></span>
<span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;fstream&gt;</span><span class="cp"></span>

<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">access_key</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;ACCESS_KEY&quot;</span><span class="p">;</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">secret_key</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;SECRET_KEY&quot;</span><span class="p">;</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">host</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;HOST&quot;</span><span class="p">;</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">sample_bucket</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;sample_bucket&quot;</span><span class="p">;</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">sample_key</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;hello.txt&quot;</span><span class="p">;</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="n">sample_file</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;resource/hello.txt&quot;</span><span class="p">;</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">security_token</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span><span class="w"></span>
<span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">auth_region</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">NULL</span><span class="p">;</span><span class="w"></span>

<span class="n">S3BucketContext</span><span class="w"> </span><span class="n">bucketContext</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">host</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">sample_bucket</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">S3ProtocolHTTP</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">S3UriStylePath</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">access_key</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">secret_key</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">security_token</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">auth_region</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>

<span class="n">S3Status</span><span class="w"> </span><span class="nf">responsePropertiesCallback</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="n">S3ResponseProperties</span><span class="w"> </span><span class="o">*</span><span class="n">properties</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">callbackData</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">S3StatusOK</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">static</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="nf">responseCompleteCallback</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="n">S3Status</span><span class="w"> </span><span class="n">status</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="n">S3ErrorDetails</span><span class="w"> </span><span class="o">*</span><span class="n">error</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">callbackData</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">S3ResponseHandler</span><span class="w"> </span><span class="n">responseHandler</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">responsePropertiesCallback</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">responseCompleteCallback</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="creating-and-closing-a-connection">
<h2>Creating (and Closing) a Connection<a class="headerlink" href="#creating-and-closing-a-connection" title="Permalink to this headline">¶</a></h2>
<p>This creates a connection so that you can interact with the server.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">S3_initialize</span><span class="p">(</span><span class="s">&quot;s3&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">S3_INIT_ALL</span><span class="p">,</span><span class="w"> </span><span class="n">host</span><span class="p">);</span><span class="w"></span>
<span class="c1">// Do stuff...</span>
<span class="n">S3_deinitialize</span><span class="p">();</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="listing-owned-buckets">
<h2>Listing Owned Buckets<a class="headerlink" href="#listing-owned-buckets" title="Permalink to this headline">¶</a></h2>
<p>This gets a list of Buckets that you own.
This also prints out the bucket name, owner ID, and display name
for each bucket.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">S3Status</span><span class="w"> </span><span class="nf">listServiceCallback</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">ownerId</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">ownerDisplayName</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">bucketName</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="kt">int64_t</span><span class="w"> </span><span class="n">creationDate</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">callbackData</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kt">bool</span><span class="w"> </span><span class="o">*</span><span class="n">header_printed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">bool</span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">callbackData</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!*</span><span class="n">header_printed</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="o">*</span><span class="n">header_printed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">true</span><span class="p">;</span><span class="w"></span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%-22s&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;       Bucket&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;  %-20s  %-12s&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;     Owner ID&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Display Name&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;----------------------&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;  --------------------&quot;</span><span class="w"> </span><span class="s">&quot;  ------------&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>

<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%-22s&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">bucketName</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;  %-20s  %-12s&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">ownerId</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nl">ownerId</span> <span class="p">:</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">ownerDisplayName</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nl">ownerDisplayName</span> <span class="p">:</span><span class="w"> </span><span class="s">&quot;&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span><span class="w"></span>

<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="n">S3StatusOK</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">S3ListServiceHandler</span><span class="w"> </span><span class="n">listServiceHandler</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">responseHandler</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">listServiceCallback</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="kt">bool</span><span class="w"> </span><span class="n">header_printed</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">false</span><span class="p">;</span><span class="w"></span>
<span class="n">S3_list_service</span><span class="p">(</span><span class="n">S3ProtocolHTTP</span><span class="p">,</span><span class="w"> </span><span class="n">access_key</span><span class="p">,</span><span class="w"> </span><span class="n">secret_key</span><span class="p">,</span><span class="w"> </span><span class="n">security_token</span><span class="p">,</span><span class="w"> </span><span class="n">host</span><span class="p">,</span><span class="w"></span>
<span class="w">            </span><span class="n">auth_region</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">listServiceHandler</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">header_printed</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="id2">
<h2>创建一个桶<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
<p>这里创建一个新桶。</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">S3_create_bucket</span><span class="p">(</span><span class="n">S3ProtocolHTTP</span><span class="p">,</span><span class="w"> </span><span class="n">access_key</span><span class="p">,</span><span class="w"> </span><span class="n">secret_key</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="n">host</span><span class="p">,</span><span class="w"> </span><span class="n">sample_bucket</span><span class="p">,</span><span class="w"> </span><span class="n">S3CannedAclPrivate</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">responseHandler</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="id3">
<h2>罗列一个桶的内容<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h2>
<p>获取桶里对象的列表。
它也会打印出各个对象的名字、文件尺寸、和最后修改日期。</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">S3Status</span><span class="w"> </span><span class="nf">listBucketCallback</span><span class="p">(</span><span class="w"></span>
<span class="w">                </span><span class="kt">int</span><span class="w"> </span><span class="n">isTruncated</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">nextMarker</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="kt">int</span><span class="w"> </span><span class="n">contentsCount</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="n">S3ListBucketContent</span><span class="w"> </span><span class="o">*</span><span class="n">contents</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="kt">int</span><span class="w"> </span><span class="n">commonPrefixesCount</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">**</span><span class="n">commonPrefixes</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">callbackData</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%-22s&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;      Object Name&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;  %-5s  %-20s&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;Size&quot;</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;   Last Modified&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;----------------------&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;  -----&quot;</span><span class="w"> </span><span class="s">&quot;  --------------------&quot;</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">contentsCount</span><span class="p">;</span><span class="w"> </span><span class="n">i</span><span class="o">++</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kt">char</span><span class="w"> </span><span class="n">timebuf</span><span class="p">[</span><span class="mi">256</span><span class="p">];</span><span class="w"></span>
<span class="w">                </span><span class="kt">char</span><span class="w"> </span><span class="n">sizebuf</span><span class="p">[</span><span class="mi">16</span><span class="p">];</span><span class="w"></span>
<span class="w">        </span><span class="k">const</span><span class="w"> </span><span class="n">S3ListBucketContent</span><span class="w"> </span><span class="o">*</span><span class="n">content</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="p">(</span><span class="n">contents</span><span class="p">[</span><span class="n">i</span><span class="p">]);</span><span class="w"></span>
<span class="w">                </span><span class="kt">time_t</span><span class="w"> </span><span class="n">t</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">time_t</span><span class="p">)</span><span class="w"> </span><span class="n">content</span><span class="o">-&gt;</span><span class="n">lastModified</span><span class="p">;</span><span class="w"></span>

<span class="w">                </span><span class="n">strftime</span><span class="p">(</span><span class="n">timebuf</span><span class="p">,</span><span class="w"> </span><span class="k">sizeof</span><span class="p">(</span><span class="n">timebuf</span><span class="p">),</span><span class="w"> </span><span class="s">&quot;%Y-%m-%dT%H:%M:%SZ&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">gmtime</span><span class="p">(</span><span class="o">&amp;</span><span class="n">t</span><span class="p">));</span><span class="w"></span>
<span class="w">                </span><span class="n">sprintf</span><span class="p">(</span><span class="n">sizebuf</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;%5llu&quot;</span><span class="p">,</span><span class="w"> </span><span class="p">(</span><span class="kt">unsigned</span><span class="w"> </span><span class="kt">long</span><span class="w"> </span><span class="kt">long</span><span class="p">)</span><span class="w"> </span><span class="n">content</span><span class="o">-&gt;</span><span class="n">size</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">printf</span><span class="p">(</span><span class="s">&quot;%-22s  %s  %s</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="w"> </span><span class="n">content</span><span class="o">-&gt;</span><span class="n">key</span><span class="p">,</span><span class="w"> </span><span class="n">sizebuf</span><span class="p">,</span><span class="w"> </span><span class="n">timebuf</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">S3StatusOK</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">S3ListBucketHandler</span><span class="w"> </span><span class="n">listBucketHandler</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">responseHandler</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">listBucketCallback</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="n">S3_list_bucket</span><span class="p">(</span><span class="o">&amp;</span><span class="n">bucketContext</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">listBucketHandler</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
<p>The output will look something like this:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">myphoto1</span><span class="o">.</span><span class="n">jpg</span> <span class="mi">251262</span>  <span class="mi">2011</span><span class="o">-</span><span class="mi">08</span><span class="o">-</span><span class="mi">08</span><span class="n">T21</span><span class="p">:</span><span class="mi">35</span><span class="p">:</span><span class="mf">48.000</span><span class="n">Z</span>
<span class="n">myphoto2</span><span class="o">.</span><span class="n">jpg</span> <span class="mi">262518</span>  <span class="mi">2011</span><span class="o">-</span><span class="mi">08</span><span class="o">-</span><span class="mi">08</span><span class="n">T21</span><span class="p">:</span><span class="mi">38</span><span class="p">:</span><span class="mf">01.000</span><span class="n">Z</span>
</pre></div>
</div>
</div>
<div class="section" id="deleting-a-bucket">
<h2>Deleting a Bucket<a class="headerlink" href="#deleting-a-bucket" title="Permalink to this headline">¶</a></h2>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The Bucket must be empty! Otherwise it won’t work!</p>
</div>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">S3_delete_bucket</span><span class="p">(</span><span class="n">S3ProtocolHTTP</span><span class="p">,</span><span class="w"> </span><span class="n">S3UriStylePath</span><span class="p">,</span><span class="w"> </span><span class="n">access_key</span><span class="p">,</span><span class="w"> </span><span class="n">secret_key</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">host</span><span class="p">,</span><span class="w"> </span><span class="n">sample_bucket</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">responseHandler</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="creating-an-object-from-a-file">
<h2>Creating an Object (from a file)<a class="headerlink" href="#creating-an-object-from-a-file" title="Permalink to this headline">¶</a></h2>
<p>This creates a file <code class="docutils literal notranslate"><span class="pre">hello.txt</span></code>.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;sys/stat.h&gt;</span><span class="cp"></span>
<span class="k">typedef</span><span class="w"> </span><span class="k">struct</span> <span class="nc">put_object_callback_data</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kt">FILE</span><span class="w"> </span><span class="o">*</span><span class="n">infile</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kt">uint64_t</span><span class="w"> </span><span class="n">contentLength</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"> </span><span class="n">put_object_callback_data</span><span class="p">;</span><span class="w"></span>


<span class="k">static</span><span class="w"> </span><span class="kt">int</span><span class="w"> </span><span class="nf">putObjectDataCallback</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">bufferSize</span><span class="p">,</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buffer</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">callbackData</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">put_object_callback_data</span><span class="w"> </span><span class="o">*</span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">put_object_callback_data</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">callbackData</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="kt">int</span><span class="w"> </span><span class="n">ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>

<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">data</span><span class="o">-&gt;</span><span class="n">contentLength</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kt">int</span><span class="w"> </span><span class="n">toRead</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">((</span><span class="n">data</span><span class="o">-&gt;</span><span class="n">contentLength</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="kt">unsigned</span><span class="p">)</span><span class="w"> </span><span class="n">bufferSize</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="p">(</span><span class="kt">unsigned</span><span class="p">)</span><span class="w"> </span><span class="nl">bufferSize</span> <span class="p">:</span><span class="w"> </span><span class="n">data</span><span class="o">-&gt;</span><span class="n">contentLength</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="n">ret</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fread</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">toRead</span><span class="p">,</span><span class="w"> </span><span class="n">data</span><span class="o">-&gt;</span><span class="n">infile</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="n">data</span><span class="o">-&gt;</span><span class="n">contentLength</span><span class="w"> </span><span class="o">-=</span><span class="w"> </span><span class="n">ret</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">return</span><span class="w"> </span><span class="n">ret</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">put_object_callback_data</span><span class="w"> </span><span class="n">data</span><span class="p">;</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">stat</span><span class="w"> </span><span class="n">statbuf</span><span class="p">;</span><span class="w"></span>
<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="n">stat</span><span class="p">(</span><span class="n">sample_file</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">statbuf</span><span class="p">)</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="mi">-1</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">ERROR: Failed to stat file %s: &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">sample_file</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">perror</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">exit</span><span class="p">(</span><span class="mi">-1</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="kt">int</span><span class="w"> </span><span class="n">contentLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">statbuf</span><span class="p">.</span><span class="n">st_size</span><span class="p">;</span><span class="w"></span>
<span class="n">data</span><span class="p">.</span><span class="n">contentLength</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">contentLength</span><span class="p">;</span><span class="w"></span>

<span class="k">if</span><span class="w"> </span><span class="p">(</span><span class="o">!</span><span class="p">(</span><span class="n">data</span><span class="p">.</span><span class="n">infile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fopen</span><span class="p">(</span><span class="n">sample_file</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;r&quot;</span><span class="p">)))</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">fprintf</span><span class="p">(</span><span class="n">stderr</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;</span><span class="se">\n</span><span class="s">ERROR: Failed to open input file %s: &quot;</span><span class="p">,</span><span class="w"> </span><span class="n">sample_file</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">perror</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">exit</span><span class="p">(</span><span class="mi">-1</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">S3PutObjectHandler</span><span class="w"> </span><span class="n">putObjectHandler</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">responseHandler</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">putObjectDataCallback</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>

<span class="n">S3_put_object</span><span class="p">(</span><span class="o">&amp;</span><span class="n">bucketContext</span><span class="p">,</span><span class="w"> </span><span class="n">sample_key</span><span class="p">,</span><span class="w"> </span><span class="n">contentLength</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">putObjectHandler</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">data</span><span class="p">);</span><span class="w"></span>
<span class="n">fclose</span><span class="p">(</span><span class="n">data</span><span class="p">.</span><span class="n">infile</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="id4">
<h2>下载一个对象（到一文件）<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h2>
<p>This downloads a file and prints the contents.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="k">static</span><span class="w"> </span><span class="n">S3Status</span><span class="w"> </span><span class="nf">getObjectDataCallback</span><span class="p">(</span><span class="kt">int</span><span class="w"> </span><span class="n">bufferSize</span><span class="p">,</span><span class="w"> </span><span class="k">const</span><span class="w"> </span><span class="kt">char</span><span class="w"> </span><span class="o">*</span><span class="n">buffer</span><span class="p">,</span><span class="w"> </span><span class="kt">void</span><span class="w"> </span><span class="o">*</span><span class="n">callbackData</span><span class="p">)</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kt">FILE</span><span class="w"> </span><span class="o">*</span><span class="n">outfile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="kt">FILE</span><span class="w"> </span><span class="o">*</span><span class="p">)</span><span class="w"> </span><span class="n">callbackData</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="kt">size_t</span><span class="w"> </span><span class="n">wrote</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">fwrite</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="n">bufferSize</span><span class="p">,</span><span class="w"> </span><span class="n">outfile</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="k">return</span><span class="w"> </span><span class="p">((</span><span class="n">wrote</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="p">(</span><span class="kt">size_t</span><span class="p">)</span><span class="w"> </span><span class="n">bufferSize</span><span class="p">)</span><span class="w"> </span><span class="o">?</span><span class="w"> </span><span class="nl">S3StatusAbortedByCallback</span> <span class="p">:</span><span class="w"> </span><span class="n">S3StatusOK</span><span class="p">);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">S3GetObjectHandler</span><span class="w"> </span><span class="n">getObjectHandler</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">responseHandler</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">getObjectDataCallback</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="kt">FILE</span><span class="w"> </span><span class="o">*</span><span class="n">outfile</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">stdout</span><span class="p">;</span><span class="w"></span>
<span class="n">S3_get_object</span><span class="p">(</span><span class="o">&amp;</span><span class="n">bucketContext</span><span class="p">,</span><span class="w"> </span><span class="n">sample_key</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">getObjectHandler</span><span class="p">,</span><span class="w"> </span><span class="n">outfile</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="delete-an-object">
<h2>Delete an Object<a class="headerlink" href="#delete-an-object" title="Permalink to this headline">¶</a></h2>
<p>This deletes an object.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="n">S3ResponseHandler</span><span class="w"> </span><span class="n">deleteResponseHandler</span><span class="w"> </span><span class="o">=</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="mi">0</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="o">&amp;</span><span class="n">responseCompleteCallback</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>
<span class="n">S3_delete_object</span><span class="p">(</span><span class="o">&amp;</span><span class="n">bucketContext</span><span class="p">,</span><span class="w"> </span><span class="n">sample_key</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">deleteResponseHandler</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="change-an-object-s-acl">
<h2>Change an Object’s ACL<a class="headerlink" href="#change-an-object-s-acl" title="Permalink to this headline">¶</a></h2>
<p>This changes an object’s ACL to grant full control to another user.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;string.h&gt;</span><span class="cp"></span>
<span class="kt">char</span><span class="w"> </span><span class="n">ownerId</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;owner&quot;</span><span class="p">;</span><span class="w"></span>
<span class="kt">char</span><span class="w"> </span><span class="n">ownerDisplayName</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;owner&quot;</span><span class="p">;</span><span class="w"></span>
<span class="kt">char</span><span class="w"> </span><span class="n">granteeId</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;grantee&quot;</span><span class="p">;</span><span class="w"></span>
<span class="kt">char</span><span class="w"> </span><span class="n">granteeDisplayName</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="s">&quot;grantee&quot;</span><span class="p">;</span><span class="w"></span>

<span class="n">S3AclGrant</span><span class="w"> </span><span class="n">grants</span><span class="p">[]</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">S3GranteeTypeCanonicalUser</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="p">{{}},</span><span class="w"></span>
<span class="w">                </span><span class="n">S3PermissionFullControl</span><span class="w"></span>
<span class="w">        </span><span class="p">},</span><span class="w"></span>
<span class="w">        </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">S3GranteeTypeCanonicalUser</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="p">{{}},</span><span class="w"></span>
<span class="w">                </span><span class="n">S3PermissionReadACP</span><span class="w"></span>
<span class="w">        </span><span class="p">},</span><span class="w"></span>
<span class="w">        </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="n">S3GranteeTypeAllUsers</span><span class="p">,</span><span class="w"></span>
<span class="w">                </span><span class="p">{{}},</span><span class="w"></span>
<span class="w">                </span><span class="n">S3PermissionRead</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="p">};</span><span class="w"></span>

<span class="n">strncpy</span><span class="p">(</span><span class="n">grants</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">grantee</span><span class="p">.</span><span class="n">canonicalUser</span><span class="p">.</span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="n">ownerId</span><span class="p">,</span><span class="w"> </span><span class="n">S3_MAX_GRANTEE_USER_ID_SIZE</span><span class="p">);</span><span class="w"></span>
<span class="n">strncpy</span><span class="p">(</span><span class="n">grants</span><span class="p">[</span><span class="mi">0</span><span class="p">].</span><span class="n">grantee</span><span class="p">.</span><span class="n">canonicalUser</span><span class="p">.</span><span class="n">displayName</span><span class="p">,</span><span class="w"> </span><span class="n">ownerDisplayName</span><span class="p">,</span><span class="w"> </span><span class="n">S3_MAX_GRANTEE_DISPLAY_NAME_SIZE</span><span class="p">);</span><span class="w"></span>

<span class="n">strncpy</span><span class="p">(</span><span class="n">grants</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="n">grantee</span><span class="p">.</span><span class="n">canonicalUser</span><span class="p">.</span><span class="n">id</span><span class="p">,</span><span class="w"> </span><span class="n">granteeId</span><span class="p">,</span><span class="w"> </span><span class="n">S3_MAX_GRANTEE_USER_ID_SIZE</span><span class="p">);</span><span class="w"></span>
<span class="n">strncpy</span><span class="p">(</span><span class="n">grants</span><span class="p">[</span><span class="mi">1</span><span class="p">].</span><span class="n">grantee</span><span class="p">.</span><span class="n">canonicalUser</span><span class="p">.</span><span class="n">displayName</span><span class="p">,</span><span class="w"> </span><span class="n">granteeDisplayName</span><span class="p">,</span><span class="w"> </span><span class="n">S3_MAX_GRANTEE_DISPLAY_NAME_SIZE</span><span class="p">);</span><span class="w"></span>

<span class="n">S3_set_acl</span><span class="p">(</span><span class="o">&amp;</span><span class="n">bucketContext</span><span class="p">,</span><span class="w"> </span><span class="n">sample_key</span><span class="p">,</span><span class="w"> </span><span class="n">ownerId</span><span class="p">,</span><span class="w"> </span><span class="n">ownerDisplayName</span><span class="p">,</span><span class="w"> </span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="n">grants</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">responseHandler</span><span class="p">,</span><span class="w"> </span><span class="mi">0</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
<div class="section" id="generate-object-download-url-signed">
<h2>Generate Object Download URL (signed)<a class="headerlink" href="#generate-object-download-url-signed" title="Permalink to this headline">¶</a></h2>
<p>This generates a signed download URL that will be valid for 5 minutes.</p>
<div class="highlight-cpp notranslate"><div class="highlight"><pre><span></span><span class="cp">#include</span><span class="w"> </span><span class="cpf">&lt;time.h&gt;</span><span class="cp"></span>
<span class="kt">char</span><span class="w"> </span><span class="n">buffer</span><span class="p">[</span><span class="n">S3_MAX_AUTHENTICATED_QUERY_STRING_SIZE</span><span class="p">];</span><span class="w"></span>
<span class="kt">int64_t</span><span class="w"> </span><span class="n">expires</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">time</span><span class="p">(</span><span class="nb">NULL</span><span class="p">)</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="mi">60</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="mi">5</span><span class="p">;</span><span class="w"> </span><span class="c1">// Current time + 5 minutes</span>

<span class="n">S3_generate_authenticated_query_string</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">bucketContext</span><span class="p">,</span><span class="w"> </span><span class="n">sample_key</span><span class="p">,</span><span class="w"> </span><span class="n">expires</span><span class="p">,</span><span class="w"> </span><span class="nb">NULL</span><span class="p">,</span><span class="w"> </span><span class="s">&quot;GET&quot;</span><span class="p">);</span><span class="w"></span>
</pre></div>
</div>
</div>
</div>



           </div>
           
          </div>
          <footer>
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
        <a href="../csharp/" class="btn btn-neutral float-right" title="C# S3 Examples" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
        <a href="../objectops/" class="btn btn-neutral float-left" title="对象操作" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
    </div>

  <hr/>

  <div role="contentinfo">
    <p>
        &#169; Copyright 2016, Ceph authors and contributors. Licensed under Creative Commons Attribution Share Alike 3.0 (CC-BY-SA-3.0).

    </p>
  </div> 

</footer>
        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>